package com.mf.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mf.model.po.RegisterPo;
import com.mf.dao.RegisterDao;
import com.mf.service.RegisterService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Register)表服务实现类
 */
@Service("registerService")
public class RegisterServiceImpl implements RegisterService {
    @Resource
    private RegisterDao registerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RegisterPo queryById(Long id) {
        return this.registerDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param registerPo 筛选条件
     * @param page       分页对象
     * @return 查询结果
     */
    @Override
    public IPage<RegisterPo> queryByPage(RegisterPo registerPo, IPage page) {
        final IPage result = this.registerDao.selectPage(page, new QueryWrapper<>(registerPo));
        return result;
    }

    /**
     * 按特征查询
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public List<RegisterPo> query(LambdaQueryWrapper queryWrapper) {
        return this.registerDao.selectList(queryWrapper);
    }

    /**
     * 按特征查询一条
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public RegisterPo querySingle(LambdaQueryWrapper queryWrapper) {
        queryWrapper.last("limit 1");
        final List<RegisterPo> list = registerDao.selectList(queryWrapper);
        return CollUtil.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 新增数据
     *
     * @param register 实例对象
     * @return 实例对象
     */
    @Override
    public RegisterPo insert(RegisterPo register) {
        this.registerDao.insert(register);
        return register;
    }

    /**
     * 修改数据
     *
     * @param registerPo 实例对象
     * @return 实例对象
     */
    @Override
    public RegisterPo updateById(RegisterPo registerPo) {
        this.registerDao.updateById(registerPo);
        return registerPo;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public RegisterPo deleteById(Long id) {
        final RegisterPo po = registerDao.selectById(id);
        if (po == null) {
            return null;
        }
        this.registerDao.deleteById(po);
        return po;
    }
}
